//
//  mainViewController.m
//  ceshiNET
//
//  Created by mk on 15/8/18.
//  Copyright (c) 2015年 mk. All rights reserved.
//

#import "mainViewController.h"
#import "RequestManager.h"
#import "ExpertTeams.h"
#import "StarTeamCollectionViewCell.h"
#import "BookingViewController.h"
@interface mainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *array;
@end

@implementation mainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = [NSMutableArray array];
    [self createView];
    [self handleData];
}
- (void)handleData{
    NSString *url = @"http://mingyizhudao.com/api/appnav2";

    [ RequestManager GET:url  success:^(id response)  {
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *expertTeamsArray = dic[@"expertTeams"];
        for (NSMutableDictionary *dic in expertTeamsArray) {
            ExpertTeams *exper= [[ExpertTeams alloc]initWithDictionary:dic];
            [self.array addObject:exper];
        }
        NSLog(@"%@",[[self.array lastObject]teamName]);
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);

    }];


}
- (void)createView{
  //  CGSize tmpSize = self.collectionView.bounds.size;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 3, 150);
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        flowLayout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    flowLayout.minimumInteritemSpacing = 1;
    flowLayout.minimumLineSpacing = 1;
    self.collectionView =[[UICollectionView alloc]initWithFrame :CGRectMake(0, 200, self.view.frame.size.width, 280) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor yellowColor];
   // self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[StarTeamCollectionViewCell class] forCellWithReuseIdentifier:@"starTeam"];
     self.collectionView.contentSize = CGSizeMake(self.view.frame.size.width / 3  * self.array.count, 0);

    NSLog(@"~~~%f",flowLayout.itemSize.height);
    NSLog(@"---%@",self.collectionView);

}
#pragma CollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"starTeam";
    StarTeamCollectionViewCell   *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Identifier forIndexPath:indexPath];
    cell.exper = [self.array objectAtIndex:indexPath.row];
    

    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BookingViewController *stVC = [[BookingViewController alloc]init];
    stVC.booking = [[self.array objectAtIndex:indexPath.row]teamName];
    [self.navigationController pushViewController:stVC animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
